package org.example.carplatoon.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.example.carplatoon.common.R;
import org.example.carplatoon.entity.car;
import org.example.carplatoon.entity.carBind;
import org.example.carplatoon.entity.user;
import org.example.carplatoon.service.carBindService;
import org.example.carplatoon.service.carService;
import org.example.carplatoon.service.userService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RequestMapping
@RestController
public class carController {

    @Autowired
    private carService carService;

    @Autowired
    private userService userService;

    @Autowired
    private carBindService carBindService;


    @GetMapping("/getUserCar")
    @Transactional
    public R<List<car>> getUserCar(long userId) {
        user user = userService.getById(userId);
        if (user == null) {
            return R.error("该用户不存在");
        }
        LambdaQueryWrapper<car> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(car::getUserId,userId);
        List<car> list = carService.list(queryWrapper);

//        todo:暂时固定图片src
//        for (car carTmp : list) {
//            String avatar = carTmp.getAvatar();
//            carTmp.setAvatar(basePath + avatar);
//        }
        if (list.isEmpty()) {
            return R.success("暂无车辆绑定，请添加",list);
        }

        return R.success("获取车辆信息成功",list);
    }

    @PostMapping("/addCar")
    @Transactional
    public R addCar(@RequestBody car newCar) {
        Long userId = newCar.getUserId();
        user user = userService.getById(userId);
        if (user == null) {
            return R.error("用户不存在");
        }
        String carPlate = newCar.getCarPlate();
        LambdaQueryWrapper<car> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(car::getCarPlate,carPlate);
        int count = carService.count(queryWrapper);
        if (count > 0) {
            return R.error("该车牌号已存在");
        }
        if ((newCar.getCarLength().compareTo(BigDecimal.ZERO) <= 0 ||
                newCar.getCarLength().compareTo(new BigDecimal(12)) > 0) ||
                (newCar.getCarHeight().compareTo(BigDecimal.ZERO) <= 0 ||
                        newCar.getCarHeight().compareTo(new BigDecimal(4)) > 0)) {
            return R.error("输入车辆长宽有误");
        }
        newCar.setAvatar("/images/car.jpg");
        carService.save(newCar);

        return R.success("车辆添加成功",null);
    }

    @PostMapping("/updatedUserCar")
    @Transactional
    public R updatedUserCar(@RequestBody car updatedCar) {
        car carById = carService.getById(updatedCar.getId());
        if (carById == null) {
            return R.error("该车辆信息不存在");
        }

        LambdaQueryWrapper<carBind> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(carBind::getCarId, carById.getId());
        carBind carBind = carBindService.getOne(lambdaQueryWrapper);
        if (carBind != null) {
            return R.error("该车辆正在运行中，无法修改");
        }

        if ((updatedCar.getCarLength().compareTo(BigDecimal.ZERO) <= 0 ||
                updatedCar.getCarLength().compareTo(new BigDecimal(12)) > 0) ||
                (updatedCar.getCarHeight().compareTo(BigDecimal.ZERO) <= 0 ||
                 updatedCar.getCarHeight().compareTo(new BigDecimal(4)) > 0)) {
            return R.error("输入车辆长宽有误");
        }
        String carPlate = updatedCar.getCarPlate();
        LambdaQueryWrapper<car> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(car::getId,updatedCar.getId()).eq(car::getCarPlate,carPlate);
        int count = carService.count(queryWrapper);
        if (count > 0) {
            return R.error("该车牌号已存在");
        }
        carService.updateById(updatedCar);

        return R.success("车辆信息修改成功",null);
    }

    @PostMapping("/deleteCar")
    @Transactional
    public R deleteCar(@RequestBody car deleteCar) {
        LambdaQueryWrapper<carBind> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(carBind::getCarId,deleteCar.getId())
                .eq(carBind::getUserId,deleteCar.getUserId());
        carBind car = carBindService.getOne(queryWrapper);
        if (car != null) {
            return R.error("该车辆正在车队中，删除失败");
        }
        car tmpCar = carService.getById(deleteCar.getId());
        if (tmpCar == null) {
            return R.success("该车辆不存在",null);
        } else if (tmpCar.getUserId().equals(deleteCar.getUserId())) {
            carService.removeById(deleteCar.getId());
            return R.success("删除成功",null);
        } else {
            return R.error("信息有误，删除失败");
        }
    }

}
